import { SetterMap, WidgetMap } from "@ali-dayu/form-editor";
import { Switch } from "antd";
import AnnexUploadField from "components/AnnexUploadField";
import OptionSetter, { optionSetterName } from "./widgetConfig/OptionSetter";
import CodeSetter, { CodeSetterName } from "./widgetConfig/CodeSetter";
import EmptySetter, { EmptySetterName } from "./widgetConfig/EmptySetter";
import selectConfig from "./widgetConfig/DDSelectField";
import checkboxConfig from "./widgetConfig/CheckBoxField";
import radioConfig from "./widgetConfig/RadioField";
import textareaConfig from "./widgetConfig/TextareaField";
import dateConfig from "./widgetConfig/DDDateField";
import dateRangeConfig from "./widgetConfig/DDDateRangeField";
import starRatingConfig from "./widgetConfig/StarRatingField";
import variableLengthTableConfig from "./widgetConfig/VariableLengthTableField";
// import formCardConfig from "./widgetConfig/FormCardField"; // form-editor内置了这个，不需要手动设置了

import WrapPropToKeyPath from "./wrapper";

const hideCodeSetterKeys = ['ColumnLayout', 'Card'];
const hideEnableAsFlowVariableSetterKeys = ['ColumnLayout', 'Card'];
const BasicSetterMap = () => {
  const arr = Object.keys(WidgetMap);
  if (arr.length > 0) {
    for (const keys of arr) {
      WidgetMap[keys].cfg.dropToColumnEnable = true;
      const config = {
        propName: "_id",
        setterName: CodeSetterName,
        props: { label: "表单编码", inputProps: { maxLength: 25 } },
      };
      const enableAsFlowVariableConfig = {
        propName: "enableAsFlowVariable",
        setterName: 'EnableAsFlowVariableSetter',
        props: { label: "是否启用变量" },
      };

      if (WidgetMap[keys].cfg.setters) {
        if (hideEnableAsFlowVariableSetterKeys.indexOf(keys) < 0) {
          WidgetMap[keys].cfg.setters.unshift(enableAsFlowVariableConfig);
          WidgetMap[keys].cfg.props.enableAsFlowVariable = false;
        } 
        if (hideCodeSetterKeys.indexOf(keys) < 0) {
          const builtIdSetters = WidgetMap[keys].cfg.setters;
          // 去除内置_id setter
          const setters = builtIdSetters.filter(({ propName}) => propName !== '_id');
          setters.unshift(config);
          WidgetMap[keys].cfg.setters = setters;
        }

        if (WidgetMap[keys].cfg.setters.length === 0) {
          WidgetMap[keys].cfg.setters.push({
            propName: "setterEmptyPlaceholder",
            setterName: EmptySetterName,
            props: {}, 
          });
        }
      }
    }
  }
  return WidgetMap;
};

const BasicWidgetMap = BasicSetterMap();

const CustomSetterMap = {
  ...SetterMap,

  TemplateFieldSetter: {
    cfg: {
      setterName: "TemplateFieldSetter",
      props: {
        // params: {
        //   multiple: true,
        // },
      },
    },
    extend: WrapPropToKeyPath(AnnexUploadField, ["template"], {
      defaultValue: [],
      extraProps: { params: { multiple: true } },
      toWrapValue: fileList => {
        return (fileList || []).map(file => {
          const { url, name } = file;
          if (url) {
            return { fileName: name, url };
          } else {
            return file;
          }
        });
      },
      toOriginValue: fileList => {
        return (fileList || []).map((file, idx) => {
          const { fileName, uid, url } = file;
          if (url) {
            return { uid: uid || fileName + -idx, name: fileName, url };
          } else {
            return file;
          }
        });
      },
    }),
  },

  WrapperSwitchSetter: {
    cfg: {
      setterName: "WrapperSwitchSetter",
      props: {},
    },
    extend: WrapPropToKeyPath(Switch, ["params", "multiple"], {
      compOriginalKey: "checked",
      defaultValue: false,
    }),
  },

  EnableAsFlowVariableSetter: {
    cfg: {
      setterName: "EnableAsFlowVariableSetter",
      props: {},
    },
    extend: WrapPropToKeyPath(Switch, null, {
      compOriginalKey: "checked",
      defaultValue: false,
      containerStyle: {
        display: 'flex',
        justifyContent: 'space-between',
        marginBottom: 24,
      },
    }),
  },

  [optionSetterName]: {
    cfg: {
      setterName: optionSetterName,
      props: {
        // params: {
        //   multiple: true,
        // },
      },
    },
    extend: OptionSetter,
  },
  [CodeSetterName]: {
    cfg: {
      setterName: CodeSetterName,
      props: {},
    },
    extend: CodeSetter,
  },
  [EmptySetterName]: {
    cfg: {
      setterName: EmptySetterName,
      props: {},
    },
    extend: EmptySetter,
  },
  ...selectConfig.setter,
  ...checkboxConfig.setter,
  ...radioConfig.setter,
  ...textareaConfig.setter,
  ...dateConfig.setter,
  ...dateRangeConfig.setter,
  ...starRatingConfig.setter,
  ...variableLengthTableConfig.setter,
};

const CustomWidgetMap = {
  ...BasicWidgetMap,
  ...selectConfig.widget,
  ...checkboxConfig.widget,
  ...radioConfig.widget,
  ...textareaConfig.widget,
  ...dateConfig.widget,
  ...dateRangeConfig.widget,
  ...starRatingConfig.widget,
  ...variableLengthTableConfig.widget,
  // ...formCardConfig.widget,
};

const selectPersonFieldWigetCfg = {
  ...WidgetMap.SelectPersonField.cfg,
  setters: [
    ...CustomWidgetMap.SelectPersonField.cfg.setters,
    {
      propName: "params",
      setterName: "WrapperSwitchSetter",
      props: {
        label: "多选",
      },
    },
  ],
  props: {
    ...CustomWidgetMap.SelectPersonField.cfg.props,
    ratio: 12,
    params: { params: { multiple: false } },
  },
};

const attachementWigetCfg = {
  ...WidgetMap.Attachment.cfg,
  setters: [
    ...CustomWidgetMap.Attachment.cfg.setters,
    {
      propName: "ratio",
      setterName: "RatioSetter",
      props: {
        label: "字段占比",
      },
    },
    {
      propName: "itemProps",
      setterName: "TemplateFieldSetter",
      props: {
        label: "附件模板",
      },
    },
    {
      propName: "params",
      setterName: "WrapperSwitchSetter",
      props: {
        label: "上传多个",
      },
    },
  ],
  props: {
    ...CustomWidgetMap.Attachment.cfg.props,
    ratio: 12,
    params: { params: { multiple: false } },
    itemProps: {
      template: [],
      // template: [{ uid: 123, name: 'abc.png', url: '/flow-platform/file/download?objectName=test/20210311/7bd29b72-4c32-4169-b4e3-04ec6b7a5351_upload.png'}],
    },
  },
};
CustomWidgetMap.Attachment = {
  ...CustomWidgetMap.Attachment,
  cfg: attachementWigetCfg,
};
CustomWidgetMap.SelectPersonField = {
  ...CustomWidgetMap.SelectPersonField,
  cfg: selectPersonFieldWigetCfg,
};

export {
  // SetterMap as CustomSetterMap,
  // WidgetMap as CustomWidgetMap,
  CustomSetterMap,
  CustomWidgetMap,
};
